package serverdata.ch.home.www.ResponseClass;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.util.Log;


import serverdata.ch.home.www.SensorCoding;
import serverdata.ch.home.www.SensorValueListener;
import serverdata.ch.home.www.server.HttpData;


public class GetSensorValueResponse {
	
	
	
	private HttpData inputHttp;
	private SensorManager sm;
	private float[] value;



	public GetSensorValueResponse(PrintWriter out, BufferedReader in, SensorManager sm, HttpData inputHttp) throws IOException {

		this.inputHttp=inputHttp;
		this.sm = sm;
		String data;
		String type = inputHttp.getParametri().get("TYPE");
		if(type != null && type.equalsIgnoreCase("html")){
			data = httpData();
		}else if(type != null && type.equalsIgnoreCase("xml")){
			data = XMLdata();
		}
		else{
			data = JSONdata();
		}
		out.print(HttpData.createHeaderHTTP(data, 200, type));

		out.print(data+"\r\n");
		out.flush();
		out.close();
		in.close();
	
	}
	
	private String JSONdata() {
		JSONObject obj = new JSONObject();
		if(!getSensorValue().equalsIgnoreCase("sensore inesistente")){
			
			try {
				obj.put("X", value[0]);
				obj.put("Y", value[1]);
				obj.put("Z", value[2]);
			} catch (JSONException e) {
				Log.e("SensorValue", "errore nella creazione del JSONObject da inviare");
			}
		}
		
		return obj.toString();
	}

	private String httpData(){
		StringBuilder data = new StringBuilder();	
		data.append("<html><title>Dati andorid</title><body>");
		data.append("<H1>valore sensori</H1>");

		data.append("<p>"+getSensorValue()+"</p>");

		data.append("</body></html>");
		
		return data.toString();
	}
	
	private String XMLdata(){
		StringBuilder data = new StringBuilder();
		data.append("<?xml version=\"1.0\"?>");
	
		if(!getSensorValue().equalsIgnoreCase("sensore inesistente")){
		
		
		data.append("<valori><x>"+value[0]+"</x><y>"+value[1]+"</y><z>"+value[2]+"</z></valori>");
		
		}
		else{
			data.append("<valori>non esistenti<valori>");
		}
		return data.toString();
		
	}
	
	
	private String getSensorValue() {

		String param = null;
		String values = "SENSORE INESISTENTE";
		SensorValueListener listener = new SensorValueListener();
		List<Sensor> sensorlist = null;

		if((param = inputHttp.getParametri().get("SENSORE")) != null){
			d(param);
			
			SensorCoding sensorCoding = new SensorCoding(param);
			System.out.println("getsensor: "+sensorCoding.getSensor());
			switch(sensorCoding.getSensor()){
			case ACC:
				d("sono in acc");
				sensorlist = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case AMBTEMP:
				sensorlist = sm.getSensorList(Sensor.TYPE_TEMPERATURE);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case GRAV:
				sensorlist = sm.getSensorList(Sensor.TYPE_GRAVITY);
				System.out.println(sensorlist);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case GYRO:
				sensorlist = sm.getSensorList(Sensor.TYPE_GYROSCOPE);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case LIGHT:

				sensorlist = sm.getSensorList(Sensor.TYPE_LIGHT);

				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case LINACC:
				sensorlist = sm.getSensorList(Sensor.TYPE_LINEAR_ACCELERATION);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case MAG:
				sensorlist = sm.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case ORIENT:
				sensorlist = sm.getSensorList(Sensor.TYPE_ORIENTATION);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case PRESS:
				sensorlist = sm.getSensorList(Sensor.TYPE_PRESSURE);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case PROX:
				sensorlist = sm.getSensorList(Sensor.TYPE_PROXIMITY);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case RELHUM:
				values = "SENSORE INESISTENTE";
				break;
			case ROTA:
				sensorlist = sm.getSensorList(Sensor.TYPE_ROTATION_VECTOR);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			case TEMP:
				sensorlist = sm.getSensorList(Sensor.TYPE_TEMPERATURE);
				if(!sensorlist.isEmpty()){
					values = returnValue(sensorlist, listener);
				}
				break;
			default:
				values = "SENSORE INESISTENTE";
				break;

			}



		}

		return values;
	}

	

	private String returnValue(List<Sensor> sensorlist, SensorValueListener listener){
		sm.registerListener(listener, sensorlist.get(0),SensorManager.SENSOR_DELAY_NORMAL);
		value = listener.getValues();
		String values = "X: "+value[0] +" Y: "+value[1] +" Z: "+value[2];
		sm.unregisterListener(listener);
		return values;
	}
	
	private void d(String msg){
		Log.d("SensorValue", msg);
	}
}
